package dao.impl;

import dao.BaseDao;
import dao.NewsDao;
import entity.News;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2019/3/19.
 */
public class NewsDaoimpl extends BaseDao implements NewsDao {
    @Override
    public List<News> getNewsList() {
        List<News> list = new ArrayList();
        ResultSet rs = null;
        try {
            if (getConnection()) {
                String sql = "select * from news_detail";
                Object[] nums = {};
                rs=excuteSelect(sql,nums);
                while (rs.next()){
                    News news=new News();
                    int id=rs.getInt("id");
                    int categoryId=rs.getInt("categoryId");
                    String title=rs.getString("title");
                    String summary=rs.getString("summary");
                    String content=rs.getString("content");
                    String author=rs.getString("author");
                    String picPath=rs.getString("picPath");
                    Timestamp createDate=rs.getTimestamp("createDate");
                    news.setId(id);
                    news.setCategoryId(categoryId);
                    news.setAuthor(author);
                    news.setContent(content);
                    news.setCreateDate(createDate);
                    news.setSummary(summary);
                    news.setPicPath(picPath);
                    news.setTitle(title);
                    list.add(news);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeConnection();
        }
        return list;
    }


    @Override
    public int delNews(int i) {
        ResultSet rs = null;
        int num = 0;
        try {
            if (getConn()) {
                String sql = "delete from news_detail where id=?";
                Object nums[] = {i};
                num = excuteUpdateRows(sql, nums);
            } else {
                System.out.println("连接失败！");
            }
        } catch (Exception e) {
        } finally {
            closeConnection();
        }
        return num;
    }

    @Override
    public News selidNews(int i) {
        News news=new News();
        try {
            ResultSet rs = null;
            if (getConnection()) {
                String sql = "select * from news_detail where id=?";
                Object[] nums = {i};
                rs = excuteSelect(sql, nums);
                while (rs.next()) {
                    int id = rs.getInt("id");
                    int categoryId = rs.getInt("categoryId");
                    String title = rs.getString("title");
                    String summary = rs.getString("summary");
                    String content = rs.getString("content");
                    String author = rs.getString("author");
                    String picPath = rs.getString("picPath");
                    Timestamp createDate = rs.getTimestamp("createDate");
                    System.out.println(id + "\t" + categoryId + "\t" + title + "\t" + summary + "\t" + content + "\t" + author + "\t" + picPath + "\t" + createDate);
                    news.setId(id);
                    news.setCategoryId(categoryId);
                    news.setAuthor(author);
                    news.setContent(content);
                    news.setCreateDate(createDate);
                    news.setSummary(summary);
                    news.setPicPath(picPath);
                    news.setTitle(title);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeConnection();
        }
        return news;
    }


    @Override
    public int upNews(News news) {
        ResultSet rs = null;
        int num = 0;
        try {
            if (getConn()) {
                String sql =  "UPDATE `kgcnews`.`news_detail` SET `categoryId` = ? , `title` = ? , `summary` = ? ,`content` = ? ,author=?,modifyDate=? WHERE `id` = ?";
                Object obj[] = {news.getCategoryId(),news.getTitle(),news.getSummary(),news.getContent(),
                        news.getAuthor(),news.getModifyDate(),news.getId()};
                num = excuteUpdateRows(sql, obj);
            } else {
                System.out.println("连接失败！");
            }
        } catch (Exception e) {
            System.out.println("出错了");
        } finally {
            closeConnection();
        }
        return num;
    }


    @Override
    public int addNews(News news) {
        int ire=0;
        try {
            if (getConnection()) {
                //创建statument执行sql
                String sql = "INSERT INTO news_detail (`categoryId`, `title`, `summary`, `content`, `author`, `createDate`) \n" +
                        "VALUES (?,?,?,?,?,?)";
                Object[] params = {news.getCategoryId(), news.getTitle(), news.getSummary(), news.getContent()
                        , news.getAuthor(), news.getCreateDate()};
                ire = this.excuteUpdateRows(sql, params);
                if (ire > 0) {
                    System.out.println("添加成功!");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeConnection();
        }
        return ire;
    }
}
